﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;

namespace System.Lua.Serialization
{
    public static class TypeExtensions
    {
        public static bool SerializeAsEnumerable(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            return !type.SerializeAsLiteral() && type.GetInterface("IEnumerable") != null;
        }

        public static bool SerializeAsArray(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            return type.IsArray;
        }

        public static bool SerializeAsClassOrStruct(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            return !type.IsArray && ((type.IsClass|type.IsValueType&&!type.IsPrimitive) && type != typeof(string));
        }

        public static bool SerializeAsLiteral(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            return type.IsPrimitive || type == typeof(string);
        }

        public static bool SerializeAsDictionary(this Type type) {
            if (type == null)
                throw new ArgumentNullException("type");
            return !type.SerializeAsLiteral() && type.GetInterface("IDictionary") != null;
        }

        public static bool IsLuaTable(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            return type==typeof(LuaInterface.LuaTable);
        }

        public static bool IsDouble(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            return type.IsPrimitive&&type==typeof(Single)|type==typeof(Double);
        }

        public static bool IsString(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            return type == typeof(String);
        }

        public static bool IsBool(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            return type.IsPrimitive && type == typeof(Boolean);
        }

        public static bool IsClassOrStruct(this LuaTable table)
        {
            if (table == null)
                throw new ArgumentNullException("table");
            bool rev = false;
            foreach (var key in table.Keys)
                if ((key.ToString()) == LuaSerializer.TypeTableAttribute)
                    rev = true;
            return rev;
        }

        public static bool IsEnumerable(this LuaTable table)
        {
            if (table == null)
                throw new ArgumentNullException("table");
            foreach (var key in table.Keys)
                if ((key.ToString()) == LuaSerializer.TypeTableAttribute)
                    return false;
            return true;
        }

    }
}
